<html>
<head><meta charset="utf-8"><title>Why does iter.position require a mutable reference? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20does.20iter.2Eposition.20require.20a.20mutable.20reference.3F.html">Why does iter.position require a mutable reference?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="230826330"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20does%20iter.position%20require%20a%20mutable%20reference%3F/near/230826330" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Oliver E. Anderson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20does.20iter.2Eposition.20require.20a.20mutable.20reference.3F.html#230826330">(Mar 18 2021 at 08:10)</a>:</h4>
<p>Hi All, I am wondering why this method: <a href="https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.position">https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.position</a> requires &amp;mut self and not just &amp;self? is the iterator storing some state or something like that while finding the index?</p>



<a name="230830480"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20does%20iter.position%20require%20a%20mutable%20reference%3F/near/230830480" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20does.20iter.2Eposition.20require.20a.20mutable.20reference.3F.html#230830480">(Mar 18 2021 at 08:55)</a>:</h4>
<p><span class="user-mention" data-user-id="390743">@Oliver E. Anderson</span> position consumes elements from <br>
the iterator until it finds an item for which the predicate is true. This requires &amp;mut.</p>



<a name="230830853"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20does%20iter.position%20require%20a%20mutable%20reference%3F/near/230830853" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20does.20iter.2Eposition.20require.20a.20mutable.20reference.3F.html#230830853">(Mar 18 2021 at 08:59)</a>:</h4>
<p>If you don't want mutable access, you can either first clone the iterator, or use an iterator generating method on the underlying <code>IntoIterator</code> type like <code>Vec::iter()</code></p>



<a name="230867817"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20does%20iter.position%20require%20a%20mutable%20reference%3F/near/230867817" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20does.20iter.2Eposition.20require.20a.20mutable.20reference.3F.html#230867817">(Mar 18 2021 at 14:00)</a>:</h4>
<p>One needs at least <code>&amp;mut</code> access to actually iterate / move forward: all the <code>Iterator</code> APIs (but for "known" / guessed in advance stuff, like <code>.len()</code>) are <code>&amp;mut</code>-based (or, equivalently, ownership-based) since its keystone method, <code>fn next</code>, is <code>&amp;mut</code>-based.</p>



<a name="230867952"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20does%20iter.position%20require%20a%20mutable%20reference%3F/near/230867952" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20does.20iter.2Eposition.20require.20a.20mutable.20reference.3F.html#230867952">(Mar 18 2021 at 14:01)</a>:</h4>
<p>But do note that you can get a mutable iterator out of shared access to a collection, <code>.iter()</code> being the canonical example.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>